package com.me.study.concurrent.thread;

/**
 * volatile 环境下：i + 1 <p>
 *     结果必定不是 1000：why？
 *     因为 volatile 不具原子性。count = count + 1;
 *     具有3个操作：
 *     1、读取 count
 *     2、count + 1
 *     3、写回 count
 *
 * @author ME
 * @date   2019/3/10 11:36
 */
public class TestVolatile2 {

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread() {
                @Override
                public void run() {
                    TestVolatile2.add();
                }
            }.start();
        }
        System.out.println(count);
    }

    private volatile static int count = 0;

    public static void add() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count = count + 1;
    }
}
